import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        // 1. 先从请求的body中读取 用户名 和密码
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        // 2. 判定 用户名密码 是否正确(这里直接固定)
        if(!"wz".equals(username) || !"123456".equals(password)){
            // 用户名和密码有错误的
            resp.getWriter().write("登录失败!");
            return;
        }
        // 3. 登录成功,则创建一个会话出来.
        HttpSession httpSession = req.getSession(true);
        httpSession.setAttribute("username","wz");
        httpSession.setAttribute("loginCount",0);
        // 4. 让页面跳转到主页,使用重定向
        resp.sendRedirect("index");
    }
}
